package cool.mtc.minghe.data.model.sys.user.form;

import cool.mtc.minghe.data.entity.sys.user.UserOrgRoleDO;
import lombok.Data;

import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 明河
 */
@Data
public class UserOrgForm {

    @NotEmpty(message = "user.org.required")
    private Long orgId;

    private List<Long> roleIdList = new ArrayList<>();

    public List<UserOrgRoleDO> transToUserOrgRoleDO(long userId) {
        List<UserOrgRoleDO> userOrgRoleDOList = new ArrayList<>();
        roleIdList.forEach(roleId -> {
            userOrgRoleDOList.add(this.getUserOrgRoleDO(userId, roleId));
        });
        if (userOrgRoleDOList.isEmpty()) {
            userOrgRoleDOList.add(this.getUserOrgRoleDO(userId, null));
        }
        return userOrgRoleDOList;
    }

    private UserOrgRoleDO getUserOrgRoleDO(long userId, Long roleId) {
        UserOrgRoleDO userOrgRoleDO = new UserOrgRoleDO();
        userOrgRoleDO.setUserId(userId);
        userOrgRoleDO.setRoleId(roleId);
        userOrgRoleDO.setOrgId(this.getOrgId());
        return userOrgRoleDO;
    }
}
